package com.hxzy.springbootes.controller;

import com.hxzy.springbootes.es.entity.EsHotel;
import com.hxzy.springbootes.es.service.EsHotalService;
import com.hxzy.springbootes.mybatis.entity.TbHotel;
import com.hxzy.springbootes.mybatis.service.TbHotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author gityyge
 * @Description TODO
 * @Date 2023/5/31 15:55
 * @Version 1.0
 */
@RestController
public class EsHotalController {

    /**
     * mysql数据库
     */
    @Autowired
    private TbHotelService  tbHotelService;

    /**
     * es搜索引库的
     */
    @Autowired
    private EsHotalService esHotalService;


    /**
     * 新增或修改
     * @param id
     * @return
     */
    @PostMapping(value = "/es/add/{id}")
    public boolean insert(@PathVariable(value = "id") Long id){
        //1、从数据库查询数据
        TbHotel db = this.tbHotelService.getById(id);
        if(db==null){
            throw new RuntimeException("查询不到数据");
        }

        //2、把db转换为es的模型
        EsHotel esHotel=new EsHotel(db);
        //3、把esHotal插入到es索引
        this.esHotalService.insert(esHotel);

        return true;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping(value = "/es/{id}")
    public EsHotel findById(@PathVariable(value = "id") Long id){
        EsHotel  esHotel=this.esHotalService.findById(id);
        if(esHotel==null){
            throw new RuntimeException("查询不到数据");
        }
        return esHotel;
    }

    /**
     * 删除es中的数据( 一般情况，要先删除数据库中的值，然后才能删除ES中的值，这里只做ES演示)
     * @param id
     * @return
     */
    @DeleteMapping(value = "/es/delete/{id}")
    public boolean deleteById(@PathVariable(value = "id") Long id){
        boolean result=this.esHotalService.deleteById(id);
        return result;
    }

}
